package com.example.stella.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.stella.models.Category;

import java.util.ArrayList;
import java.util.List;

public class CategoryDAO {
    private DatabaseHelper dbHelper;

    public CategoryDAO(Context context) {
        dbHelper = new DatabaseHelper(context);
    }

    public List<Category> getCategoriesByType(String type) {
        List<Category> categories = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String selection = DatabaseHelper.COLUMN_TYPE + " = ?";
        String[] selectionArgs = {type};
        
        Cursor cursor = db.query(DatabaseHelper.TABLE_CATEGORIES, null, selection, selectionArgs, null, null, null);
        
        while (cursor.moveToNext()) {
            Category category = new Category(
                cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_ID)),
                cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_NAME)),
                cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_TYPE)),
                cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_ICON))
            );
            categories.add(category);
        }
        
        cursor.close();
        db.close();
        return categories;
    }

    public List<Category> getAllCategories() {
        List<Category> categories = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        
        Cursor cursor = db.query(DatabaseHelper.TABLE_CATEGORIES, null, null, null, null, null, null);
        
        while (cursor.moveToNext()) {
            Category category = new Category(
                cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_ID)),
                cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_NAME)),
                cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_TYPE)),
                cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_ICON))
            );
            categories.add(category);
        }
        
        cursor.close();
        db.close();
        return categories;
    }
} 